package cn.chiship.framework.business.biz.global;


import cn.chiship.framework.business.biz.business.entity.TestExample;
import cn.chiship.framework.business.biz.business.service.TestExampleService;
import cn.chiship.framework.business.core.common.BusinessCommonConstants;
import cn.chiship.framework.common.annotation.SystemOptionAnnotation;
import cn.chiship.framework.upms.biz.system.service.UpmsProofService;
import cn.chiship.framework.upms.biz.system.service.UpmsSmsCodeService;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.enums.BaseResultEnum;
import cn.chiship.sdk.core.id.SnowflakeIdUtil;
import cn.chiship.sdk.core.util.DateUtils;
import cn.chiship.sdk.core.util.OutUtil;
import cn.chiship.sdk.core.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * controller
 * 2020/8/5
 *
 * @author lijian
 */
@RestController
@RequestMapping("/export")
@Api(tags = "数据导出控制器")
public class ExportController {

    private static final String STRING_ASYNC = "async";
    private static final String STRING_CLASS_NAME = "className";
    @Resource
    private UpmsSmsCodeService upmsSmsCodeService;

    @Resource
    private UpmsProofService upmsProofService;

    @Resource
    private TestExampleService testExampleService;

    @SystemOptionAnnotation(describe = "根据类与参数集合导出数据")
    @ApiOperation(value = "根据类与参数集合导出数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramsMap", value = "数据集合", required = true, dataTypeClass = Map.class, paramType = "body")
    })
    @PostMapping(value = "exportData")
    public void exportData(
            @RequestBody Map<String, Object> paramsMap,
            HttpServletResponse response) throws Exception {
        String exportType = paramsMap.get("exportType").toString();
        String className = paramsMap.get(STRING_CLASS_NAME).toString();
        if (StringUtil.isNullOrEmpty(exportType)) {
            BaseResult baseResult = BaseResult.error(BaseResultEnum.FAILED, "缺少参数[exportType]");
            OutUtil.writeJson(response, baseResult);
        }
        if (StringUtil.isNullOrEmpty(className)) {
            BaseResult baseResult = BaseResult.error(BaseResultEnum.FAILED, "缺少参数[className]");
            OutUtil.writeJson(response, baseResult);
        }
        paramsMap.remove(STRING_CLASS_NAME);
        boolean async = false;
        if (paramsMap.containsKey(STRING_ASYNC)) {
            try {
                async = Boolean.valueOf(paramsMap.get(STRING_ASYNC).toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        String taskId = DateUtils.dateTimeNow() + SnowflakeIdUtil.generateId();
        switch (className) {
            /**
             * 导出短信
             */
            case BusinessCommonConstants.EXPORT_IMPORT_CLASS_SMS_CODE:
                if (async) {
                    upmsSmsCodeService.asyncExportData(response, taskId, paramsMap);
                } else {
                    upmsSmsCodeService.exportData(response, paramsMap);
                }
                break;
            /**
             * 导出密钥
             */
            case BusinessCommonConstants.EXPORT_IMPORT_CLASS_PROOF:
                if (async) {
                    upmsProofService.asyncExportData(response, taskId, paramsMap);
                } else {
                    upmsProofService.exportData(response, paramsMap);
                }
                break;
            /**
             * 导出测试示例
             */
            case BusinessCommonConstants.EXPORT_IMPORT_CLASS_TEST_EXAMPLE:
                if (async) {
                    testExampleService.asyncExportData(response, taskId, paramsMap);
                } else {
                    testExampleService.exportData(response, paramsMap);
                }
                break;
            default:
                BaseResult baseResult = BaseResult.error(BaseResultEnum.FAILED, "不支持的导出此表:" + className + "，请在" + this.getClass().getSimpleName() + "配置业务!");
                OutUtil.writeJson(response, baseResult);

        }
        if (async) {
            OutUtil.writeJson(response, BaseResult.ok(taskId));
        }
    }

    @ApiOperation(value = "根据任务ID获取异步任务实时进度")
    @ApiImplicitParams({})
    @PostMapping(value = "getProcessStatus")
    public void getProcessStatus(HttpServletResponse response, @RequestBody Map<String, Object> paramsMap) throws Exception {
        String taskId = paramsMap.get("taskId").toString();
        String className = paramsMap.get(STRING_CLASS_NAME).toString();
        if (StringUtil.isNullOrEmpty(taskId)) {
            BaseResult baseResult = BaseResult.error(BaseResultEnum.FAILED, "缺少参数[taskId]");
            OutUtil.writeJson(response, baseResult);
        }
        if (StringUtil.isNullOrEmpty(className)) {
            BaseResult baseResult = BaseResult.error(BaseResultEnum.FAILED, "缺少参数[className]");
            OutUtil.writeJson(response, baseResult);
        }
        switch (className) {
            /**
             * 导出短信
             */
            case BusinessCommonConstants.EXPORT_IMPORT_CLASS_SMS_CODE:
                upmsSmsCodeService.getExportProcessStatus(response, taskId);
                break;
            /**
             * 导出密钥
             */
            case BusinessCommonConstants.EXPORT_IMPORT_CLASS_PROOF:
                upmsProofService.getExportProcessStatus(response, taskId);
                break;
            /**
             * 导出测试示例
             */
            case BusinessCommonConstants.EXPORT_IMPORT_CLASS_TEST_EXAMPLE:
                testExampleService.getExportProcessStatus(response, taskId);
                break;
            default:
                OutUtil.writeJson(response, BaseResult.error(BaseResultEnum.FAILED, "不支持的查看此表[" + className + "]进度"));
        }
    }
}
